package com.qdxwx.manage.controller.course;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.course.CourseStudyHistoryService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.course.CourseStudyHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 课程播放记录(学习记录)Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/course/study")
public class CourseStudyHistoryController extends BaseController {

    @Autowired
    private CourseStudyHistoryService courseStudyHistoryService;

    @GetMapping("/list.html")
    public String history() {
        return "course/study-history";
    }

    /**
     * 查询课程播放记录(学习记录)列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        int searchType = TKConvert.toInteger(request.getParameter("searchType"));
        String searchKey = TKString.trim(request.getParameter("searchKey"));
        CourseStudyHistory courseStudyHistory = new CourseStudyHistory();
        if (searchKey.length() > 1) {
            if (TKString.isNumber(searchKey)){
                if (searchType == 1) {
                    courseStudyHistory.setNodeId(TKConvert.toInteger(searchKey));
                } else {
                    courseStudyHistory.setCourseId(TKConvert.toInteger(searchKey));
                }
            }
            else {
                searchKey = TKString.queryKey(searchKey);
                if (searchType == 1) {
                    courseStudyHistory.setNodeName(searchKey);
                } else {
                    courseStudyHistory.setCourseName(searchKey);
                }
            }
        }
        List<CourseStudyHistory> list = courseStudyHistoryService.getList(courseStudyHistory, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(courseStudyHistoryService.count(courseStudyHistory));
        return rspData;
    }
}
